/*
 * @Author: Superficial
 * @Date: 2019-12-15 14:25:15
 * @LastEditTime: 2021-02-22 16:30:37
 * @Description: uaParser
 */

import parser from 'ua-parser-js';

/**
 * @description: 通过userAgent获取操作系统和浏览器信息
 * @param {String} userAgent userAgent信息
 * @return {Object} parseResult 返回操作系统和浏览器信息
 */
const uaParser = function (userAgent) {
  const parseResult = parser(userAgent || '');
  const browserName = String(parseResult.browser.name).toLowerCase();
  const isBrowser = (browsers) => {
    return browsers.some((browser) => browser.toLowerCase() === browserName);
  };
  
  return {
    result: parseResult,
    isIE: isBrowser(['compatible', 'MSIE', 'IE']),
    isEdge: isBrowser(['Edge']),
    isFirefox: isBrowser(['Firefox']),
    isChrome: isBrowser(['Chrome', 'Chromium']),
    isSafari: isBrowser(['Safari']),
    isWechat: isBrowser(['Wechat']),
    isIos: parseResult.os.name === 'iOS',
    isAndroid: parseResult.os.name === 'Android',
    isMobile: parseResult.device.type === 'mobile',
  };
};

export default uaParser;
